package 技巧;
import java.util.Arrays;
/**
 * @author aodre , QQ : 480029069
 * @date 2023/8/16 16:04
 */
public class 循环数组 {

    /*
     正逆序 遍历 循环数组
     */
    public static void main(String[] args) {
        int[] circularArray = {5, 2, 9, 1, 7};
        int startIndex = 2; // 起始索引位置

        System.out.println("正序遍历环形数组：");
        traverseCircularArray(circularArray, startIndex, true);

        System.out.println("\n倒序遍历环形数组：");
        traverseCircularArray(circularArray, startIndex, false);
    }

    public static void traverseCircularArray(int[] arr, int startIndex, boolean isForward) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            int index = (startIndex + i) % length;
            if (!isForward) {
                index = (startIndex - i + length) % length;
            }
            System.out.print(arr[index] + " ");
        }
    }

}
